package com.dfec.book.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfec.book.dao.BookCategoryDao;
import com.dfec.book.dao.BookDao;
import com.dfec.book.entity.BookCategoryEntity;
import com.dfec.book.entity.BookEntity;
import com.dfec.book.exception.ServiceException;
import com.dfec.book.service.BookCategoryService;
import com.dfec.book.util.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 图书类别服务实现类
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-06-19 09:46:23
 */
@Service
public class BookCategoryServiceImpl extends ServiceImpl<BookCategoryDao, BookCategoryEntity> implements BookCategoryService {

    @Resource
    private BookDao bookDao;


    @Override
    public PageUtils getBookCategoryPage(BookCategoryEntity bookCategoryEntity) {
        Page<BookCategoryEntity> page = new Page<>(bookCategoryEntity.getPageNo(), bookCategoryEntity.getPageSize());
        IPage<BookCategoryEntity> bookCategoryEntityIPage = baseMapper.selectPage(page, bookCategoryEntity);
        return new PageUtils(bookCategoryEntityIPage);
    }

    @Override
    public List<BookCategoryEntity> getAllCategories() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean addCategory(BookCategoryEntity category) {

        //先校验类别名称不能为空
        String categoryName = category.getCategoryName();
        if (StringUtils.isBlank(categoryName)) {
            throw new ServiceException("类别名称不能为空");
        }
        // 检查名称是否重复
        BookCategoryEntity bookCategoryEntity = baseMapper.selectByName(categoryName);
        if (!Objects.isNull(bookCategoryEntity)) {
            throw new ServiceException("类别名称已存在");
        }
        return save(category);
    }

    @Override
    public boolean deleteCategory(List<Integer> categoryIds) {
        // 检查类别下是否有图书
        for (Integer categoryId : categoryIds) {
            BookEntity bookEntity = new BookEntity();
            bookEntity.setCategoryId(categoryId);
            List<BookEntity> bookEntities = bookDao.selectByExample(bookEntity);
            if (!bookEntities.isEmpty()) {
                throw new ServiceException("该类别下存在图书，无法删除");
            }
        }
        return removeByIds(categoryIds);
    }

    @Override
    public BookCategoryEntity findCategoryByName(String categoryName) {
        return baseMapper.selectByName(categoryName);
    }

    @Override
    public boolean updateCategory(BookCategoryEntity category) {
        //检查更新的ID不能为空，因为更新数据是根据ID来更新的
        if(category.getCategoryId() == null){
            throw new ServiceException("类别ID不能为空");
        }
        // 检查名称是否与其他类别重复
        BookCategoryEntity bookCategoryEntity = baseMapper.selectByName(category.getCategoryName());
        if (!Objects.isNull(bookCategoryEntity)) {
            throw new ServiceException("类别名称已存在");
        }
        return updateById(category);
    }

    @Override
    public boolean existsById(Integer categoryId) {
        if (categoryId == null) {
            throw new ServiceException("类别ID不能为空");
        }
        BookCategoryEntity bookCategoryEntity = baseMapper.selectById(categoryId);
        if (Objects.isNull(bookCategoryEntity)) {
            return false;
        }
        return true;
    }
}
